<?php

require_once "dbconnect.php";

$state_name = $_POST['state'];
if( empty($state_name) ) $state_name = $_GET['state'];

$only_active = $_POST['is_active'];
if( empty($only_active) ) $only_active = $_GET['is_active'];

//echo "state_name = ".$state_name."<br>";

if( !empty($state_name) ){
	//get sub categories of the category with id = parent_id
	$sql = "SELECT c.id, c.name, c.state, c.country, c.driver_fee, c.position  FROM city c WHERE c.state = '".$state_name."' AND c.is_active = '1'";
}
else {
	//get all categories
	$sql = "SELECT * FROM city WHERE is_active = '1'";
}
$res = $db->query($sql);

//For debug:
//echo "Total categories found: ".$res->num_rows;
//echo "<br>";

if( $res->num_rows == 0 ){
	echo json_encode(array("status"=>"no city found"));
	exit();
}
for( $i=0; $i<$res->num_rows; $i++ ){
	$city = array(
		"id"=>$res->rows[$i]['id'],
		"name"=>$res->rows[$i]['name'],
		"state"=>$res->rows[$i]['state'],
		"country"=>$res->rows[$i]['country'],
		"driver_fee"=>$res->rows[$i]['driver_fee'],
		"position"=>$res->rows[$i]['position'],
		"is_active"=>$res->rows[$i]['is_active']
	);
	$cities[] = $city;
}
echo json_encode(array("status"=>"ok","cities"=>$cities));

?>
